<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAdddialog(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editdialog(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="deletedialog(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加子部门 -->
    <el-dialog
      :title="dialogTitle"
      :visible="adddialog"
      width="50%"
      @close="closeAddDialog"
    >
      <el-form ref="addform" label-width="120px" :model="addform" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addform.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="addform.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="addform.manager" style="width:80%" placeholder="请选择">
            <el-option
              v-for="item in simpleList"
              :key="item.id"
              :value="item.username"
              :label="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="addform.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import targetArr from '@/utils/transTree'
import { getDepartmentlist, getUsersimple, postDepartment, deleteDepartment, getDepartment, putDepartment } from '@/api/Department'
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (this.codeArr.includes(value)) {
        callback(new Error('code重复'))
      } else {
        callback()
      }
    }
    return {
      list: [{
        name: '财务部',
        children: [
          {
            name: '财务核算部'
          },
          {
            name: '税务核算部'
          }
        ]
      }],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      adddialog: false, // 添加子部门
      dialogTitle: '',
      addform: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validatePass, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      simpleList: [],
      codeArr: []
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    // 组织架构列表
    async getlist() {
      const res = await getDepartmentlist()
      this.list = targetArr(res.depts)
      this.arr = res.depts
      this.arr.forEach(item => {
        this.codeArr.push(item.code)
      })
    },
    // 添加子部门
    openAdddialog(id) {
      this.dialogTitle = '新增部门'
      this.addform.pid = id
      this.adddialog = true
      this.getsimple()
    },
    // 关闭添加dialog
    closeAddDialog() {
      this.adddialog = false
      this.$refs.addform.resetFields()
    },
    // 取消按钮
    cancel() {
      this.adddialog = false
      this.$refs.addform.resetFields()
    },
    // 获取负责人
    async getsimple() {
      const res = await getUsersimple()
      this.simpleList = res
    },
    // 提交表单添加
    submitForm() {
      this.$refs.addform.validate(async valid => {
        if (valid) {
          if (this.addform.id) {
            await putDepartment(this.addform)
          } else {
            await postDepartment(this.addform)
          }
          this.$message.success(`${this.addform.id ? '编辑' : '新增'}成功`)
          this.$refs.addform.resetFields()
          this.adddialog = false
          this.getlist()
          this.resetaddform()
        }
      })
    },
    // 删除按钮
    async deletedialog(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        await deleteDepartment(id)
        this.$message.success('删除操作成功')
        this.getlist()
      })
    },
    // 编辑按钮
    async editdialog(id) {
      this.dialogTitle = '编辑部门'
      this.adddialog = true
      const res = await getDepartment(id)
      this.addform = res
      this.codeArr = this.codeArr.filter(item => {
        return item !== res.code
      })
    },
    // 重置表单
    resetaddform() {
      this.addform = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
    }
  }
}
</script>

<style>

</style>
